package com.sunlands.zlcx.usercenter.service.status;

import com.sunlands.zlcx.usercenter.constant.LearningStatus;
import com.sunlands.zlcx.usercenter.constant.PublisherStatus;
import com.sunlands.zlcx.usercenter.domain.UsersTargetRelationDO;
import com.sunlands.zlcx.usercenter.exception.UserTimeLearningException;
import com.sunlands.zlcx.usercenter.exception.UserNotLearningException;
import com.sunlands.zlcx.usercenter.service.status.common.AbstractStudyStatus;
import com.sunlands.zlcx.usercenter.vo.UsersStudyInfoVO;
import com.sunlands.zlcx.usercenter.vo.UsersVO;
import com.sunlands.zlcx.usercenter.vo.feign.RoomPublisherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import java.util.Date;
import static com.sunlands.zlcx.usercenter.config.RedisConfig.CacheKeys.USER_TARGET_BY_USERID;
import static com.sunlands.zlcx.usercenter.constant.LearningStatus.LIAN_MAI_TIMING;

/**
 * @author gaoyanfeng
 * @create 2019-04-26 18:07
 **/
@Component
@Slf4j
public class LianMaiCountTime extends AbstractStudyStatus {
    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = USER_TARGET_BY_USERID, key = "#studyInfo.userId", condition = "#studyInfo.userId != null"),
    })
    public void handle(UsersStudyInfoVO studyInfo, UsersTargetRelationDO targetRelation) {
        Long userId = targetRelation.getUserId();
        UsersVO users = usersService.findOne(userId);
        LearningStatus learningStatus = LearningStatus.getLearningStatus(users.getStatus());

        if (learningStatus != LIAN_MAI_TIMING) {
            throw new UserNotLearningException("用户不是" + LIAN_MAI_TIMING.getTips() + users.toString() + "learningStatus = " + learningStatus);
        }
        log.debug("usersStudyInfo = {} users = {}", studyInfo, users);

        if (studyInfo.getEndTime().getTime() < users.getStartLearnTime().getTime()) {
            throw new IllegalArgumentException("连麦结束时间大于开始时间，userId = " + studyInfo.getUserId());
        }
        if (null == studyInfo.getLianMaiId()) {
            throw new IllegalArgumentException("连麦id参数为空 lianmaiId = " + studyInfo.getLianMaiId());
        }
        Date lastLearningEndTime = usersLearnTimingRepository.getLastLearningEndTime(userId);
        if (null != lastLearningEndTime && studyInfo.getEndTime().getTime() < lastLearningEndTime.getTime()) {
            throw new UserTimeLearningException("用户在该时段重复学习");
        }
        RoomPublisherVO publishRecord = liveVideoService.getPublishRecordId(studyInfo.getLianMaiId()).getData();
        log.info("调用连麦系统接口 publishRecord ={}", publishRecord);

        Date agreeTime = publishRecord.getAgreeTime();
        Date quitTime = publishRecord.getQuitTime();
        int publishedSeconds = publishRecord.getPublishedSeconds().intValue();
        Integer publisherState = publishRecord.getPublisherState();
        Long publishUserId = publishRecord.getUserId();

        //如果用户已经退出或者放假内的状态异常，设置默认参数
        if ((publisherState != PublisherStatus.QUIT.getCode() && publisherState != PublisherStatus.FORCEQUIT.getCode())
                || publishUserId.longValue() != userId) {
            agreeTime = new Date();
            quitTime = new Date();
            publishedSeconds = 60;
        }

        studyInfo.setStartTime(agreeTime);
        studyInfo.setEndTime(quitTime);
        studyInfo.setStudyMinuteSeconds(publishedSeconds);
        learnTimingProcess(studyInfo, targetRelation, userId);

    }

}
